//
//  TencentQQ.m
//  Unity-iPhone
//
//  Created by YunQue on 2025/4/25.
//

#import <Foundation/Foundation.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "TencentQQ.h"
#import "GameHelper.h"

@implementation TencentQQ

TencentOAuth *_tencentOAuth;

static TencentQQ *instance;

+(instancetype) Instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[TencentQQ alloc] init];
    });
    return instance;
}

-(void) Init
{
    NSLog(@"QQ初始化成功！");
    _tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1106988065" andDelegate:self];
}

-(void) Login
{
    NSArray* permission = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO,kOPEN_PERMISSION_GET_USER_INFO,kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
    [_tencentOAuth authorize:permission];
}

-(void) Logout
{
    [_tencentOAuth logout:self];
}

-(BOOL)CheckAuthorVaild
{
    NSUserDefaults *login = [NSUserDefaults standardUserDefaults];
    NSString *unionid = [login objectForKey:@"QQunionid"];
    NSString *token =[_tencentOAuth getCachedToken];
    NSString *openid =[_tencentOAuth getCachedOpenID];
    NSDate *expries =[_tencentOAuth getCachedExpirationDate];
    [_tencentOAuth setAccessToken:token];
    [_tencentOAuth setOpenId:openid];
    [_tencentOAuth setExpirationDate:expries];
    [_tencentOAuth setUnionid:unionid];
    NSLog(@"是否授权有效：%d",_tencentOAuth.isCachedTokenValid);
    return _tencentOAuth.isCachedTokenValid;
}

-(NSString *)GetAuthor
{
    NSString* token = _tencentOAuth.accessToken;
    NSString* openid =_tencentOAuth.openId;
    NSString* unionid = _tencentOAuth.unionid;
    NSLog(@"unioid:%@",unionid);
    NSString* refreshtoken = @"";
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString* expires = [format stringFromDate:_tencentOAuth.expirationDate];
    NSString* paytoken = @"";
    NSString* pf = @"";
    NSString* pfkey = @"";
    NSString* expirestime = @"";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                openid,@"openid",
                                token,@"token",
                                unionid,@"unionid",
                                refreshtoken,@"refreshtoken",
                                expires,@"expires",
                                paytoken,@"paytoken",
                                pf,@"pf",
                                pfkey,@"pfkey",
                                expirestime,@"expirestime",
                                nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    NSUserDefaults *login = [NSUserDefaults standardUserDefaults];
    [login setObject:unionid forKey:@"QQunionid"];
    [login synchronize];
    return jsonString;
}

- (void)tencentDidLogin
{
    if(_tencentOAuth.accessToken && [_tencentOAuth.accessToken length]!=0)
    {
        NSLog(@"登陆成功！");
        BOOL request = [_tencentOAuth RequestUnionId];
        if(!request)
        {
            NSLog(@"请求Unionid失败！");
        }
    }
    else
    {
        NSLog(@"登陆失败！");
        _SendPlatformMessageToUnity(PLATFORM_MSG_QQLOGINCALLBACK, 1, 0, 0, @"", @"",@"");
    }
}

- (void)tencentDidNotLogin:(BOOL)cancelled
{
    if(cancelled)
    {
        NSLog(@"用户取消登陆！");
        _SendPlatformMessageToUnity(PLATFORM_MSG_QQLOGINCALLBACK, 2, 0, 0, @"", @"",@"");
    }
    else
    {
        NSLog(@"登陆失败！");
        _SendPlatformMessageToUnity(PLATFORM_MSG_QQLOGINCALLBACK, 1, 0, 0, @"", @"",@"");
    }
}

- (void)tencentDidNotNetWork
{
    NSLog(@"无网络链接，登陆失败！");
    _SendPlatformMessageToUnity(PLATFORM_MSG_QQLOGINCALLBACK, 1, 0, 0, @"", @"",@"");
}

-(void)didGetUnionID
{
    NSString *jsonString= [self GetAuthor];
    NSLog(@"请求票据成功：%@",jsonString);
    _SendPlatformMessageToUnity(PLATFORM_MSG_QQLOGINCALLBACK, 0, 0, 0, jsonString, @"", @"");
}

@end
